
module.exports = {
    
    /**
    *  用户登录
    */
    async login() {
        const data:any = await this.getUserInfo()
        let code = await this.getCode()
        let openId = await this.getOpenId()
        let res = await wx.$request({
            code,...openId, ...data.userInfo
        }, wx.$api.wechat_login);
        if (res) {
            wx.setStorageSync('login_key', res.data.login_key);
            wx.navigateBack({
                delta: 1
            });
        }
    },
    /**
    *  获取openId
    */
    async getOpenId() {
        console.log(await this.getCode());
        const openId=wx.getStorageSync("openId");
          
        return openId.openid ? openId: wx.$request({
            code: await this.getCode()
        }, wx.$api.getOpenId).then((res:any) => {
            wx.setStorageSync("openId", res.data);
              
            return res.data
        });
    },
    /**
    *  获取code
    */
    getCode() {
        return new Promise((resolve, reject) => {
            wx.login({
                success: resolve,
                fail: reject
            })
        }).then((res:any) => res.code)
    },
    /**
    *  获取用户信息
    */
    getUserInfo() {
        return new Promise((resolve, reject) => {
            wx.getUserInfo({
                lang: "zh_CN",
                success: resolve,
                fail: reject
            })
        }).then(res => res)
    }
}